/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.B;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.MaintainSign;
import org.springblade.modules.repair.enums.SignEnum;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.IMaintainSignService;
import org.springblade.modules.repair.vo.MaintainSignVO;
import org.springblade.modules.repair.vo.PlanSignVO;
import org.springblade.modules.repair.wrapper.MaintainSignWrapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 维保签到明细 控制器
 *
 * @author zcc
 * @since 2021-05-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/maintain/sign")
@Api(value = "维保签到明细", tags = "维保签到明细接口")
public class MaintainSignController extends BladeController {

	private final IMaintainSignService maintainSignService;
	private final IMaintainPlanContentService planContentService;
	private final RedisTemplate redisTemplate;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入maintainSign")
	public R<MaintainSignVO> detail(MaintainSign maintainSign) {
		MaintainSign detail = maintainSignService.getOne(Condition.getQueryWrapper(maintainSign));
		return R.data(MaintainSignWrapper.build().entityVO(detail));
	}


	/**
	 * 签到接口
	 */
	@ApiLog("工单-人员作业签到")
	@TenantIgnore
	@PostMapping("/in")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入maintainSign")
	public R<Boolean> sign(@Valid @RequestBody MaintainSign maintainSign) {
		Boolean aBoolean = getAddScoreWorkId(Convert.toStr(maintainSign.getPlanId()));
		if (aBoolean) {
			maintainSignService.sign(maintainSign);
		} else {
			throw new ServiceException("操作过快...");
		}
		return R.status(Boolean.TRUE);
	}

	/**
	 * 签退
	 * @param maintainSign
	 * @return
	 */
	@ApiLog("工单-人员作业签退")
	@PostMapping("/out")
	public R<Boolean> signOut(@RequestBody MaintainSign maintainSign){
		return R.data(maintainSignService.signOut(maintainSign));
	}

//	@GetMapping("/user/detail")
//	public R<MaintainSignVO> userSignDetail(Long planId,Long userId){
//		MaintainSign sign = maintainSignService.getOne(Wrappers.<MaintainSign>lambdaQuery().eq(MaintainSign::getUserId,userId).eq(MaintainSign::getElevatorPlanId,planId).eq(MaintainSign::getStatus, SignEnum.SIGN.getId()));
//		return R.data(MaintainSignWrapper.build().entityVO(sign));
//	}

	/**
	 * 获取任务签到记录
	 * @param planId
	 * @return
	 */
	@GetMapping("/list")
	public R<List<PlanSignVO>> listPlanSign(Long planId){
		return R.data(maintainSignService.listPlanSign(planId));
	}

	/**
	 * 签到前检查该任务的电梯和签到人是否有未完成的任务
	 * @param userId
	 * @return
	 */
	@GetMapping("/plan/check")
	public R<HashMap<String,Object>> signPlanCheck(Long userId,Long planId){
		return R.data(maintainSignService.signPlanCheck(userId,planId));
	}

	/**
	 * 判断用户是否为该任务最后一个签退
	 * @param maintainSign
	 * @return
	 */
	@GetMapping("/check/last/user")
	public R<Boolean> checkUserLastSign(MaintainSign maintainSign){
		return R.data(maintainSignService.checkUserLastSign(maintainSign));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   工单离线签到同步
	 * }
	 * @Date 14:56 2023/7/14
	 * @Param
	 * @return
	 **/
	@TenantIgnore
	@PostMapping("/offline/sign")
	public R<Boolean> offlineSign(@RequestBody MaintainSign sign){
		return R.data(maintainSignService.offlineSign(sign));
	}

	/**
	 * 校验保养评分项工作项
	 *
	 * @param workId 工作项id
	 * @return 数据集
	 */
	private synchronized Boolean getAddScoreWorkId(String workId) {
		String toStr = Convert.toStr(redisTemplate.opsForValue().get(workId));
		if (StrUtil.isEmpty(toStr)) {
			redisTemplate.opsForValue().set(workId, workId, 1, TimeUnit.SECONDS);
			return true;
		} else {
			return false;
		}
	}



}
